
#include "delay.h"


#if 0

void delay_us(u32 nus)
{
    u32 start,stop,ticks;
    start=CORE_CNT;
    ticks = nus * CORE_CLK_MHZ/4;// cnt timer is core_clk/4
    while(CORE_CNT-start<ticks){}
}
#else

//version 1
void delay_us(u32 nus)
{
    u32 start,stop,ticks;
    start=CORE_CNT;
    ticks = nus * CORE_CLK_MHZ/4;// cnt timer is core_clk/4
    if(0xFFFFFFFF-start>=ticks){
        stop=start+ticks;
        while(CORE_CNT<stop && CORE_CNT>start){}
    }
    else{
        stop=ticks-(0xFFFFFFFF-start);
        while(CORE_CNT>start){}
        while(CORE_CNT<stop){}
    }
}
#endif

//ms delay surport 0xFFFFFFFFms
void HAL_Delay(u32 Delay)
{
    while(Delay>80000){
        delay_us(80000*1000);
        Delay-=80000;
    }
    delay_us(Delay*1000);
}
